﻿using CommonTools;
using Newtonsoft.Json;
using shop.Bll;
using shop.Bll.cost;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace shop.cost
{
    public partial class DaifaQtyPostEditor : Form, IEditItemDialog<DaifaQtyPostDto>
    {
        public DaifaQtyPostDto Item { set; get; }
        DaifaQtyPostBll _bll = new DaifaQtyPostBll();

        IEnumerable<string> _provs= null;


        public DaifaQtyPostEditor()
        {
            InitializeComponent();


            cl_qty.ValueType= typeof(decimal);
            box_BeginDate.Value = null;
            box_EndDate.Value= null;

        }

        private async void btnOk_Click(object sender, EventArgs e)
        {
            if(Item == null)
            {
                Item = new DaifaQtyPostDto();
            }

            Item.BeginDate = box_BeginDate.Value;
            Item.EndDate = box_EndDate.Value;
            Item.Stock = box_stock.Text.Trim();
            Item.Province = box_provinceJson.Text.Trim();
            Item.FUser = UserBll.CurrentUser.Name;

            var sb = new StringBuilder();
            foreach (DataGridViewRow row in grid_spec.Rows)
            {
                if (row.IsNewRow)
                    break;

                sb.Append(row.Cells[cl_spec_no.Index].Value.IsNull());
                sb.Append(",");
            }
            Item.spec_no = sb.ToString().Trim(',');

            Item.PriceList = new List<QtyPriceRange>();
            foreach (DataGridViewRow row in gridPrice.Rows)
            {
                if (row.IsNewRow)
                    break;

                var com = new QtyPriceRange()
                {
                    Qty = row.Cells[cl_qty.Index].Value.IsNull(0),
                    Price = row.Cells[cl_postPrice.Index].Value.IsNull(0m),
                };

                Item.PriceList.Add(com);
            }


            if(Item.BeginDate == null)
            {
                MessageBox.Show("起始日期不能为空!", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            if (Item.EndDate == null) Item.EndDate = new DateTime(2099, 1, 1);

            if(string.IsNullOrEmpty(Item.Stock))
            {
                MessageBox.Show("仓库不能为空!", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            Result res = await _bll.SaveItem(Item);

            if (res.ErrorCode != ResultCode.Ok)
            {
                MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private  void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void btn_delete_Click(object sender, EventArgs e)
        {
            if (Item == null || Item.id <= 0)
            {
                MessageBox.Show("不能执行此操作", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var res = await _bll.DeleteItem(Item.id);

            if (res.ErrorCode != ResultCode.Ok)
            {
                MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private async void PostPriceEditor_Load(object sender, EventArgs e)
        {
           

            if (Item != null)
            {
                if (Item.id > 0)
                {
                    Item = await _bll.GetItem(Item.id);
                }

                box_BeginDate.Value = Item.BeginDate;
                box_EndDate.Value = Item.EndDate;
                box_stock.Text = Item.Stock;
                box_provinceJson.Text = Item.Province;
                
                box_FUser.Text = Item.FUser;

                var specList = Item.spec_no?.Split(',',' ');
                if(specList != null)
                {
                    grid_spec.AddItems(specList);
                }


                if(Item.PriceList != null)
                {
                    var priceList = Item.PriceList;
                    if (priceList != null && priceList.Count>0 )
                    {
                        gridPrice.AddItems(priceList,
                            a => a.Qty.ToString("#0"),
                            a => a.Price.ToString("#0.####"));
                    }
                }
                
            }
            else
            {
                //box_enable.Checked = true;
            }
        }


    }
}
